/*****************************************************************************
*    ME32G032_timer.h:  Header file for ME32G032_timer.c
*		ver 1.0
******************************************************************************/
#ifndef __ME32G030_TIMER_H 
#define __ME32G030_TIMER_H
#include "ME32G030.h"

//Int Status
#define TIM_MATCH_0_INT 0x1
#define TIM_MATCH_1_INT 0x2
#define TIM_MATCH_2_INT 0x4
#define TIM_MATCH_3_INT 0x8
#define TIM_CAPTURE_0_INT 0x10
#define TIM_CAPTURE_1_INT 0x20
#define TIM_CAPTURE_2_INT 0x40
#define TIM_CAPTURE_3_INT	 0x80

//Capture control
#define TIM_CAPTURE_DO_NOTHING 0x0
#define TIM_CAPTURE_RISE_EDGE 0x1
#define TIM_CAPTURE_FALL_EDGE 0x2
#define TIM_CAPTURE_TRIGGER_INT	 0x4

//Match Control
#define TIM_MATCH_TRIGGER_INT 0x1
#define TIM_MATCH_RESET_COUNTER 0x2
#define TIM_MATCH_STOP_COUNTER 0x4
#define TIM_MATCH_DO_NOTHING 0x0

//Match Output Control
#define TIM_MATCH_OUT_DO_NOTHING 0x0
#define TIM_MATCH_OUT_RESET 0x1
#define TIM_MATCH_OUT_SET	 0x2
#define TIM_MATCH_OUT_TOGGLE	 0x3

//Match PWM Output Channel
#define TIM_PWM0_ON_MAT0 0x1
#define TIM_PWM1_ON_MAT1 0x2
#define TIM_PWM2_ON_MAT2 0x4

//Macros
#define TIM_START(ct) 					ct->TCR_b.CEN=1;ct->TCR_b.CRST=1;ct->TCR_b.CRST=0;
#define TIM_STOP(ct) 						(ct->TCR_b.CEN = 0)
#define TIM_RESET_COUNTER(ct)  	(ct->TCR_b.CRST = 1)

void BTIM_Init(BTIM0_Type *ct, uint32_t tickpersecond);
void BTIM_ConfigMatch0(BTIM0_Type *ct, uint16_t ticks, uint8_t action);
void BTIM_ConfigMatch1(BTIM0_Type *ct, uint16_t ticks, uint8_t action);
void BTIM_SetPWMDT(BTIM0_Type *ct, uint16_t dt0, uint16_t dt1);
void BTIM_SetTimerCounter(BTIM0_Type *ct, uint16_t tick);
void BTIM_ResetTimerCounter(BTIM0_Type *ct);
uint16_t BTIM_GetTimerCounter(BTIM0_Type *ct);
void BTIM_ClearIntFlag(BTIM0_Type *ct);
void BTIM_EnableInt(BTIM0_Type *ct);
void BTIM_DisableInt(BTIM0_Type *ct);

void CTIM_Init(CTIM0_Type *ct, uint32_t tickpersecond);
void CTIM_SetCTType(CTIM0_Type *ct, uint8_t mode);
void CTIM_SelectTimerClearSignal(CTIM0_Type *ct, uint8_t capsignal, uint8_t edge);
void CTIM_SelectPriSrc(CTIM0_Type *ct, uint8_t clksrc);
void CTIM_SelectSecSrc (CTIM0_Type *ct, uint8_t clksrc, uint8_t polarity);
void CTIM_ConfigMatch0(CTIM0_Type *ct, uint16_t ticks, uint8_t internalaction, uint8_t externalaction);
void CTIM_ConfigMatch1(CTIM0_Type *ct, uint16_t ticks, uint8_t internalaction, uint8_t externalaction);
void CTIM_ConfigMatch2(CTIM0_Type *ct, uint16_t ticks, uint8_t internalaction, uint8_t externalaction);
void CTIM_ConfigMatch3(CTIM0_Type *ct, uint16_t ticks, uint8_t internalaction, uint8_t externalaction);
void CTIM_ConfigCapture0(CTIM0_Type *ct, uint8_t edge, uint8_t intreq,uint8_t filter);
void CTIM_ConfigCapture1(CTIM0_Type *ct, uint8_t edge, uint8_t intreq,uint8_t filter);
void CTIM_ConfigCapture2(CTIM0_Type *ct, uint8_t edge, uint8_t intreq,uint8_t filter);
void CTIM_ConfigCapture3(CTIM0_Type *ct, uint8_t edge, uint8_t intreq,uint8_t filter);
uint16_t CTIM_GetCapture0Value (CTIM0_Type *ct);
uint16_t CTIM_GetCapture1Value (CTIM0_Type *ct);
uint16_t CTIM_GetCapture2Value (CTIM0_Type *ct);
uint16_t CTIM_GetCapture3Value (CTIM0_Type *ct);
void CTIM_SetTimerCounter(CTIM0_Type *ct, uint16_t ticks);
void CTIM_ResetTimerCounter(CTIM0_Type *ct);
uint16_t CTIM_GetTimerCounter (CTIM0_Type *ct);
void CTIM_ClearIntFlag(CTIM0_Type *ct);
void CTIM_EnableInt(CTIM0_Type *ct, uint8_t intbit);
void CTIM_DisableInt(CTIM0_Type *ct, uint8_t intbit);
uint8_t CTIM_GetIntStatus(CTIM0_Type *ct);
void CTIM_PWMInit(CTIM0_Type *ct, uint32_t pwmfreqinhz);
void CTIM_EnablePWMOutput(CTIM0_Type *ct, uint8_t pwmchannels);
void CTIM_DisablePWM(CTIM0_Type *ct, uint8_t pwmchannels);
void CTIM_SetPWM0Duty(CTIM0_Type *ct, uint8_t duty);
void CTIM_SetPWM1Duty(CTIM0_Type *ct, uint8_t duty);
void CTIM_SetPWM2Duty(CTIM0_Type *ct, uint8_t duty);
#endif /* end __ME32G032_TIMER_H */
/*****************************************************************************
**                            End Of File
******************************************************************************/
